package com.example.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wangbin
 */
public class JSON {
    public static String toJson(Object input) throws JsonProcessingException {
        ObjectMapper om = new ObjectMapper();
        return om.writeValueAsString(input);
    }

    public static <T> T fromJson(String input, Class<T> classType) throws IOException {
        ObjectMapper om = new ObjectMapper();
        return om.readValue(input, classType);
    }

    public static Map<String, Object> toMap(String input) throws IOException {
        ObjectMapper om = new ObjectMapper();
        return om.readValue(input, HashMap.class);
    }
}
